<?php
namespace Home\Controller;
use Think\Controller;
use Think\Model;
use Home\Model\ContentCollectModel;
use Home\Event\DBEvent;
class SystemController extends Controller {

    	
     //备份数据,0：系统配置，1:采集数据
    public function writeDB(){
    	if(session('name')){
    		$tables = array();
    		$filename = null;
    		if(I('param.type',0)==0){
    			$tables = array('spider_alert_rule','spider_task','spider_search_engine');
    			$filename = 'system_config_'. date("Y-m-d-H-i-s");
    		}else if(I('param.type',0)==1){
    			$tables = array('spider_content_collect','spider_user_read');
    			$filename = 'content_'. date("Y-m-d-H-i-s");
    		}
    		DBEvent::dbDump($tables,$filename,I('param.type',1));
    	}else{
    		echo json_encode(array('result'=>'登录失效重新登陆','code'=>0));
    	}
    }
    
    //还原数据
    public function readDB(){
    	DBEvent::restore(I('param.path','')); 
    }
    
    //修改数据删除规则
    public function updateRule(){
    	if(session('name')){
    		$day = I('param.day',30);
    		DBEvent::updateDataRule($day);
    	}else{
			echo json_encode ( array (
					'result' => '登录失效重新登陆',
					'code' => 0 
			) );
		}
	}
	
	// 删除
	public function deleteDB() {
		if (session ( 'name' )) {
			$path = I ( 'param.path' );
			$id = I ( 'param.id' );
			if (is_file ( $path )) {
				if (unlink ( $path )) {
					$dataTable = M ( 'DataBackup' );
					$dataTable->delete ( $id );
					echo json_encode ( array (
							'result' => '删除成功',
							'code' => 1 
					) );
				} else {
					echo json_encode ( array (
							'result' => '文件不存在删除失败',
							'code' => 0 
					) );
				}
			} else {
				echo json_encode ( array (
						'result' => '文件不存在删除失败',
						'code' => 0 
				) );
			}
		} else{
    		echo json_encode(array('result'=>'登录失效重新登陆','code'=>0));
    	}
    }
		
		// 查询
	public function selectDB() {
		if (session ( 'name' )) {
			$dataTable = M ( 'DataBackup' );
			$dataArr = $dataTable->select();
			echo json_encode ( array (
					'result' => $dataArr,
					'code' => 1
			) );
		} else {
			echo json_encode ( array (
					'result' => '登录失效重新登陆',
					'code' => 0 
			) );
		}
	}
    
    
   
}